<template>
  <!--  elementplus 的容器-->
  <el-container class="layout-container">
    <!-- 顶部菜单 -->
    <el-header>
      <div class="el-header__logo"></div>
      <!-- element菜单标签 -->
      <div class="menu-container">
        <el-menu
            active-text-color="#1890ff"
            background-color="#fff"
            text-color="#333"
            mode="horizontal"
            router
            :collapse="false"
        >
          <el-menu-item index="/workBench/customerIndex">
            <el-icon>
              <Management />
            </el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/buyHouses/BuyHousesCuslist">
            <el-icon>
              <Bell />
            </el-icon>
            <span>买房</span>
          </el-menu-item>
          <el-menu-item index="/RentingHouses/RentingHousesCuslist">
            <el-icon>
              <OfficeBuilding />
            </el-icon>
            <span>租房</span>
          </el-menu-item>
          <el-menu-item index="/noticeManagement/NoticeManagementCuslist">
            <el-icon>
              <Money />
            </el-icon>
            <span>通知</span>
          </el-menu-item>
          <el-sub-menu>
            <template #title>
              <el-icon>
                <UserFilled />
              </el-icon>
              <span>个人中心</span>
            </template>
            <el-menu-item index="/userCus/UserInfoCus">
              <el-icon>
                <User />
              </el-icon>
              <span>基本资料</span>
            </el-menu-item>
            <el-menu-item index="/userCus/UserAvatarCus">
              <el-icon>
                <Crop />
              </el-icon>
              <span>更换头像</span>
            </el-menu-item>
            <el-menu-item index="/userCus/UserResetPasswordCus">
              <el-icon>
                <EditPen />
              </el-icon>
              <span>重置密码</span>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </div>
      <div class="right-wrapper">
        <div>客户：<strong>{{ userInfoStore.info.nickname }}</strong></div>
        <div>
          <!-- 下拉菜单 -->
          <el-dropdown placement="bottom-end" @command="handleCommand">
            <span class="el-dropdown__box">
              <el-avatar :src="userInfoStore.info.userPic ? userInfoStore.info.userPic : avatar" />
              <el-icon>
                <CaretBottom />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="UserInfoCus" :icon="User">基本资料</el-dropdown-item>
                <el-dropdown-item command="UserAvatarCus" :icon="Crop">更换头像</el-dropdown-item>
                <el-dropdown-item command="UserResetPasswordCus" :icon="EditPen">重置密码</el-dropdown-item>
                <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>
    <!-- 中间主区域 -->
    <el-main>
      <router-view></router-view>
    </el-main>
    <!-- 底部区域 -->
    <el-footer>慧家房产中介系统 ©2025 Created by 栗子</el-footer>
  </el-container>
</template>

<script setup>
import {
  Management,
  Bell,
  OfficeBuilding,
  Money,
  UserFilled,
  User,
  Crop,
  EditPen,
  SwitchButton,
  CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
// 导入接口函数
import { userInfoGetService } from '@/api/user.js'
// 导入pinia
import { useUserInfoStore } from '@/stores/userinfo.js'

const userInfoStore = useUserInfoStore()

import { ref } from 'vue'

// 获取个人信息
const getUserInf = async () => {
  let result = await userInfoGetService()
  // 存储pinia
  userInfoStore.setInfo(result.res)
}
getUserInf()

// 条目点击
// dropDown条目被点击后，回调的函数
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useTokenStore } from '@/stores/token.js'
const tokenStore = useTokenStore()
const router = useRouter()
const handleCommand = (command) => {
  if (command === 'logout') {
    // 退出登录
    ElMessageBox.confirm(
        '你确认退出登录码？',
        '温馨提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }
    )
        .then(async () => {
          // 用户点击了确认
          // 清空pinia中的token和个人信息
          userInfoStore.removeInfo()
          tokenStore.removeToken()
          // 跳转到登录页
          router.push('/')
          ElMessage({
            type: 'success',
            message: '退出成功'
          })
        })
        .catch(() => {
          // 用户点击了取消
          ElMessage({
            type: 'info',
            message: '取消退出'
          })
        })
  } else {
    // 路由
    router.push('/userCus/' + command)
  }
}
</script>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  background-color: #f5f5f5;

  .el-header {
    .menu-container {
      flex: 1; /* 占据剩余空间 */
      min-width: 0; /* 允许内容收缩 */
      overflow: hidden; /* 隐藏滚动条 */

      .el-menu {
        border-bottom: none;
        display: flex;
        flex-wrap: nowrap; /* 禁止换行 */

        /* 调整菜单项间距 */
        :deep(.el-menu-item),
        :deep(.el-sub-menu__title) {
          padding: 0 40px !important; /* 调小间距 */
        }
      }
    }
    .right-wrapper {
      flex-shrink: 0; /* 禁止右侧区域收缩 */
      display: flex;
      align-items: center;
      gap: 20px;
    }
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

    &__logo {
      width: 120px;
      height: 100%;
      background: url('@/assets/logoTop.png') no-repeat center / 120px auto;
    }

    .el-dropdown__box {
      display: flex;
      align-items: center;
      color: #333;

      .el-icon {
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }
  .el-main {
    padding: 20px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    margin: 20px;
  }
  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
    border-top: 1px solid #eee;
  }
}
</style>
